package cn.winwintech.Incense.dao.impl;

import org.springframework.stereotype.Repository;

import com.base.webapp.respm.ResException;

import cn.winwintech.Incense.dao.TreeDao;
import cn.winwintech.Incense.model.Tree;
import cn.winwintech.Incense.model.TreeType;

@Repository
public class TreeDaoImpl extends GenericDaoImpl implements TreeDao {

	@Override
	public Tree findTreeById(String id) {
		Tree tree = persistence.find(Tree.class, id);
		if (tree == null) {
			tree = new Tree();
		}
		return tree;
	}

	@Override
	public Tree findTreeByTokenNotNull(String token) throws ResException {
		return null;
	}

	@Override
	public TreeType findTreeType(int id) {
		return persistence.find(TreeType.class, id);
	}

	@Override
	public Tree saveTree(String treeId, int treeType, Double latitude, Double longitude) {
		Tree tree = persistence.find(Tree.class, treeId);
		if (tree == null) {
			tree = new Tree();
			TreeType type = persistence.find(TreeType.class, treeType);
			tree.setTreeId(treeId);
			tree.setTraCodeId(treeId);
			tree.setType(type);
			tree.setLatitude(latitude);
			tree.setLongitude(longitude);
			persistence.save(tree);
		} else if(latitude > 0){
			tree.setLatitude(latitude);
			tree.setLongitude(longitude);
			persistence.save(tree);
		}
	
		return tree;
	}

}
